package com.inool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inool.mapper.StudentMapper;
import com.inool.model.Student;
import com.inool.service.StudentService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Override
    public Student addStudent(Student student) {
        save(student);
        return student;
    }

    @Override
    public Student updateStudent(String id, Student student) {
        student.setStudentNumber(id);
        updateById(student);
        return student;
    }

    @Override
    public void deleteStudent(String id) {
        removeById(id);
    }

    @Override
    public Student getStudentById(String id) {
        return getById(id);
    }

    @Override
    public List<Student> getAllStudents() {
        return list();
    }

    @Override
    public Page<Student> pageStudent(long current, long size,
                                     String studentNumber, String name, String title,
                                     Date startTime, Date endTime) {
        Page<Student> page = new Page<>(current, size);
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        if (studentNumber != null && !studentNumber.equals("")) {
            wrapper.eq("student_number", studentNumber);
        }
        if (name != null && !name.equals("")) {
            wrapper.like("name", name);
        }
        if (title != null && !title.equals("")) {
            wrapper.like("title", title);
        }
        if (startTime != null && !startTime.equals("")) {
            wrapper.ge("start_time", startTime);
        }
        if (endTime != null && !endTime.equals("")) {
            wrapper.le("end_time", endTime);
        }
        this.page(page, wrapper);
        return page;
    }


}    